﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace PhoneAppCheckSensors
{
    using System.Collections;
    using System.Collections.Generic;

    /// <summary>
    /// 折れ線グラフパーツ
    /// XAMLで全てのプロパティを設定しておくこと
    /// </summary>
    public class GraphPart : Canvas
    {
        public double UnitWidth { get; set; }
        public Color DotColor { get; set; }
        public Color AxisColor { get; set; }
        /// <summary>
        /// 0を真ん中にしたい  ⇒ 0.5
        /// 0を下限にしたい    ⇒ 1.0
        /// 0を上限にしたい    ⇒ 0.0
        /// </summary>
        public double VerticalLevel { get; set; }

        private List<Point> points = new List<Point>();
        private double minY = 1000;
        private double maxY = -1000;
        private double widthPixels;
        private double unitHeight;

        public void AdjustDrawArea()
        {
            widthPixels = 1 / (this.ActualWidth * UnitWidth);
        }

        public void AdjustDrawArea(double unitWidth)
        {
            UnitWidth = unitWidth;
            AdjustDrawArea();
        }

        public void AddPoint(DateTimeOffset t, double value)
        {
            Point p = new Point();
            p.Y = value;
            p.X = t.Ticks;

            if (minY > p.Y)
            {
                minY = p.Y;
            }
            if (maxY < p.Y)
            {
                maxY = p.Y;
            }
            if (points.Count > 0)
            {
                if ((p.X - points[0].X) / UnitWidth >= this.ActualWidth)
                {
                    points.RemoveAt(0);
                }
            }
            points.Add(p);
        }

        public void DrawGraph()
        {
            this.Children.Clear();
            Line axis = new Line();
            axis.X1 = 0;
            axis.Y1 = this.ActualHeight * VerticalLevel;
            axis.X2 = this.ActualWidth;
            axis.Y2 = axis.Y1;
            axis.StrokeThickness = 1;
            axis.Stroke = new SolidColorBrush(AxisColor);
            this.Children.Add(axis);

            double valueX = 0.0;
            double valueY = 0.0;
            if (maxY > minY)
            {
                double halfHeight = System.Math.Max(System.Math.Abs(minY), System.Math.Abs(maxY));
                unitHeight = this.ActualHeight / (halfHeight * 2);
                valueY = this.ActualHeight * VerticalLevel - points[0].Y * unitHeight;
            }
            else
            {
                unitHeight = this.ActualHeight * VerticalLevel;
                if (minY < 0)
                {
                    valueY = this.ActualHeight;
                }
                else if (minY > 0)
                {
                    valueY = 0;
                }
                else
                {
                    valueY = this.ActualHeight / 2;
                }
            }
            for (int i = 1; i < points.Count; i++)
            {
                Line line = new Line();
                line.X1 = valueX;
                line.Y1 = valueY;
                valueX = (points[i].X - points[0].X) / UnitWidth;
                valueY = this.ActualHeight *VerticalLevel - points[i].Y * unitHeight;
                line.X2 = valueX;
                line.Y2 = valueY;
                line.StrokeThickness = 4;
                line.Stroke = new SolidColorBrush(DotColor);
                this.Children.Add(line);
            }

            TextBlock minTb = new TextBlock();
            minTb.FontSize = 16;
            minTb.Text = String.Format("{0:0.000}", minY);
            Canvas.SetLeft(minTb, this.ActualWidth-40);
            Canvas.SetTop(minTb, this.ActualHeight-20);
            this.Children.Add(minTb);

            TextBlock maxTb = new TextBlock();
            maxTb.FontSize = 16;
            maxTb.Text = String.Format("{0:0.000}", maxY);
            Canvas.SetLeft(maxTb, this.ActualWidth - 40);
            Canvas.SetTop(maxTb, 0);
            this.Children.Add(maxTb);
        }
    }
}
